<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑资料</title>
    <link rel="stylesheet" href="/static/css/profile_edit.css"type="text/css">
</head>
<body>
<div class="profile-edit-container">
    <h2 class="profile-edit-title">编辑资料</h2>
    <form class="profile-edit-form" method="post" action="/user/profile/edit_post" enctype="multipart/form-data">
        <div class="profile-edit-row">
            <label for="username">昵称</label>
            <input type="text" id="username" name="username" value="{{.User.Username}}" placeholder="请输入昵称">
        </div>
        <div class="profile-edit-row">
            <label for="avatar">头像</label>
            <div class="avatar-upload">
                <img src="{{.User.Avatar}}" alt="头像" class="avatar-img">
                <input type="file" id="avatar" name="avatar" title="上传头像">
            </div>
        </div>
        <div class="profile-edit-row">
            <label for="bio">简介</label>
            <input type="text" id="bio" name="bio" value="{{.User.Bio}}" placeholder="一句话介绍自己">
        </div>
        <div class="profile-edit-row">
            <label for="gender">性别</label>
            <select id="gender" name="gender" title="请选择性别">
                <option value="">请选择</option>
                <option value="男" {{if eq .User.Gender "男"}}selected{{end}}>男</option>
                <option value="女" {{if eq .User.Gender "女"}}selected{{end}}>女</option>
                <option value="其他" {{if eq .User.Gender "其他"}}selected{{end}}>其他</option>
            </select>
        </div>
        <div class="profile-edit-row">
            <label for="birthday">生日</label>
            <input type="date" id="birthday" name="birthday" value="{{.User.Birthday}}" title="请选择生日">
        </div>
        <div class="profile-edit-row">
            <label for="like">喜欢的</label>
            <input type="text" id="like" name="like" value="{{.User.Like}}" placeholder="喜欢的内容">
        </div>
        <div class="profile-edit-row">
            <label for="school">院校名称</label>
            <input type="text" id="school" name="school" value="{{.User.School}}" placeholder="请输入院校名称">
        </div>
        <div class="profile-edit-row">
            <label for="zhima">芝麻信用</label>
            <input type="text" id="zhima" name="zhima" value="{{.User.Zhima}}" placeholder="请输入芝麻信用">
        </div>
        <div class="profile-edit-row">
            <button type="submit" class="profile-edit-btn">保存</button>
        </div>
    </form>
</div>
<script>
    const PROFILE_CENTER_URL = '/user/center';

    document.querySelector('.profile-edit-form').onsubmit = async function(e) {
        e.preventDefault();
        const formData = new FormData(this);

        const res = await fetch(this.action, { method: 'POST', body: formData });

        let data;
        try {
            data = await res.json();
        } catch (err) {
            alert('操作失败：' + await res.text());
            return;
        }

        if (data.success) {
            alert('资料修改成功！');
            window.location.href = PROFILE_CENTER_URL;
        } else if (data.error) {
            alert('操作失败：' + data.error);
        } else {
            alert('操作失败，未知错误');
        }
    };
</script>
</body>
</html>
